package com.bite.blog.config;

import com.bite.blog.enums.ResponseCodeEnum;
import com.bite.blog.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
@RestControllerAdvice
public class ResponseError {
    @ExceptionHandler
    public <T> Response<T> handle(Exception e) {
        log.error("发生异常, e: ", e);
        return Response.fail("发生异常：" + e.getMessage());
    }

    @ExceptionHandler
    public <T> Response<T> handle(UnsupportedOperationException e) {
        log.error("传入非法参数, {}", e.getMessage());
        return Response.fail(ResponseCodeEnum.PARAM_INVALID.getCode(), "传入非法参数：" + e.getMessage());
    }

    @ExceptionHandler
    public <T> Response<T> handle(NoResourceFoundException e) {
//        log.error("未发现该资源, {}", e.getMessage());
        return Response.fail(ResponseCodeEnum.NO_RESOURCES.getCode(), "未发现该资源：" + e.getResourcePath());
    }
}
